package com.xiaoshuidi.cloud.module.iot.dal.repository;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.mybatis.core.dataobject.BaseDO;
import com.xiaoshuidi.cloud.module.iot.dal.mapper.TbDoorLockPasswordMapper;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbAccountDo;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDeviceDosageDo;
import com.xiaoshuidi.cloud.module.iot.dal.pojo.TbDoorlockPassword;
import org.springframework.stereotype.Repository;

import java.util.Comparator;
import java.util.List;

/**
 * @Author Feng
 * @date 2023/6/16
 */

@Repository
public class TbDoorLockPasswordRepository extends ServiceImpl<TbDoorLockPasswordMapper, TbDoorlockPassword> {

    /**
     * 根据设备编号查找密码列表
     *
     * @return
     */
    public List<TbDoorlockPassword> findDoorLockPasswordByDeviceNo(String deviceNo) {
        return this.baseMapper.findDoorLockPasswordByDeviceNo(deviceNo);
    }

    /**
     * 根据门锁编号和模糊匹配密码ID查找密码
     *
     * @param deviceNo
     * @param thirdPasswrodId
     * @return
     */
    public TbDoorlockPassword getDoorLockPasswordByDeviceNo(String deviceNo, String thirdPasswrodId) {
        return baseMapper.getDoorLockPasswordByDeviceNo(deviceNo, thirdPasswrodId);
    }

    /**
     * 根据主键修改不为null的属性值
     *
     * @param tbDoorlockPassword
     */
    public void updateByPrimaryKeySelective(TbDoorlockPassword tbDoorlockPassword) {
        this.updateById(tbDoorlockPassword);
    }

    public TbDoorlockPassword selectTbDoorlockPassword(TbDoorlockPassword tbDoorlockPassword) {
        QueryWrapper<TbDoorlockPassword> query = Wrappers.query();
        query.setEntity(tbDoorlockPassword);
        List<TbDoorlockPassword> select = this.baseMapper.selectList(query);
        if (CollectionUtil.isNotEmpty(select)) {
            return select.stream().max(Comparator.comparingLong(TbDoorlockPassword::getId)).get();
        }
        return null;
    }

    public List<TbDoorlockPassword> findByDeviceOrderBy(String deviceNo) {

        LambdaQueryWrapper<TbDoorlockPassword> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbDoorlockPassword::getDeviceNo, deviceNo)
                .orderByDesc(TbDoorlockPassword::getThirdPasswordId);
        List<TbDoorlockPassword> tbDoorlockPasswords = this.list(wrapper);
        return tbDoorlockPasswords;
    }

    public void updateDoorLockPasswordRecordByDeviceNoAndPasswordId(TbDoorlockPassword tbDoorlockPassword) {
        if (StrUtil.isEmpty(tbDoorlockPassword.getDeviceNo())) {
            return;
        }
        LambdaQueryWrapper<TbDoorlockPassword> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbDoorlockPassword::getThirdPasswordId, tbDoorlockPassword.getThirdPasswordId())
                .eq(TbDoorlockPassword::getDeviceNo, tbDoorlockPassword.getDeviceNo());
        tbDoorlockPassword.setDeviceNo(null);
        tbDoorlockPassword.setThirdPasswordId(null);
        this.update(tbDoorlockPassword, wrapper);
    }

    public List<TbDoorlockPassword> findByDeviceAndCreateUser(String deviceNo, String mobile) {

        LambdaQueryWrapper<TbDoorlockPassword> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbDoorlockPassword::getDeviceNo, deviceNo)
                .eq(TbDoorlockPassword::getStatus, 1)
                .eq(StrUtil.isNotEmpty(mobile), TbDoorlockPassword::getMemberMobile, mobile)
                .orderByDesc(BaseDO::getCreateTime);
        List<TbDoorlockPassword> tbDoorlockPasswords = this.list(wrapper);
        return tbDoorlockPasswords;
    }

    public Long getCountNum(TbDoorlockPassword tbDoorlockPassword) {
        QueryWrapper<TbDoorlockPassword> query = Wrappers.query();
        query.eq("status", 1);
        query.eq("password_status", 1);
        query.eq("device_no", tbDoorlockPassword.getDeviceNo());
        query.eq("member_mobile", tbDoorlockPassword.getMemberMobile());
        query.eq("deleted", 0);
        return this.baseMapper.selectCount(query);
    }

    public List<TbDoorlockPassword> getPassWordByMobile(String deviceNo, String mobile) {

        LambdaQueryWrapper<TbDoorlockPassword> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbDoorlockPassword::getDeviceNo, deviceNo)
                .eq(TbDoorlockPassword::getStatus, 1)
                .eq(TbDoorlockPassword::getMemberMobile, mobile)
                .orderByDesc(BaseDO::getCreateTime);
        List<TbDoorlockPassword> tbDoorlockPasswords = this.list(wrapper);
        return tbDoorlockPasswords;
    }

    public TbDoorlockPassword getPassWordThirdPassWordId(String deviceNo, String thirdPassWordId) {
        LambdaQueryWrapper<TbDoorlockPassword> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbDoorlockPassword::getDeviceNo, deviceNo)
                .eq(TbDoorlockPassword::getThirdPasswordId, thirdPassWordId);
        wrapper.eq(TbDoorlockPassword::getPasswordStatus, 1);
        wrapper.eq(TbDoorlockPassword::getDeleted, 0);
        wrapper.eq(TbDoorlockPassword::getStatus, 1);
        List<TbDoorlockPassword> list = this.list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    public TbDoorlockPassword getByDeviceNoAndMemberMobile(String deviceNo, String memberMobile) {
        LambdaQueryWrapper<TbDoorlockPassword> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbDoorlockPassword::getDeviceNo, deviceNo)
                .eq(TbDoorlockPassword::getMemberMobile, memberMobile);
        wrapper.eq(TbDoorlockPassword::getPasswordStatus, 1);
        wrapper.eq(TbDoorlockPassword::getDeleted, 0);
        wrapper.eq(TbDoorlockPassword::getStatus, 1);
        List<TbDoorlockPassword> list = this.list(wrapper);
        if (CollectionUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
    public Boolean updateTianchengPasswordByPasswordId(TbDoorlockPassword tbDoorlockPassword){
        LambdaQueryWrapper<TbDoorlockPassword> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(TbDoorlockPassword::getThirdPasswordId, tbDoorlockPassword.getThirdPasswordId());
        return this.update(tbDoorlockPassword, wrapper);

    }
    public int deleteIsurpassDoorLockPasswordRecordByDeviceNoAndPasswordId(TbDoorlockPassword tbDoorlockPassword){
        QueryWrapper<TbDoorlockPassword> wrapper = new QueryWrapper();
        wrapper.eq("device_no",tbDoorlockPassword.getDeviceNo());
        wrapper.eq("third_password_id",tbDoorlockPassword.getThirdPasswordId());
        return this.baseMapper.delete(wrapper);
    }

    public void updateDoorLockPasswordRecordByCondition(TbDoorlockPassword tbDoorlockPassword) {
        QueryWrapper<TbDoorlockPassword> wrapper = new QueryWrapper();
        wrapper.eq("deviceNo", tbDoorlockPassword.getDeviceNo());
        wrapper.eq("thirdPasswordId", tbDoorlockPassword.getThirdPasswordId());
        wrapper.eq("status", "1");
        tbDoorlockPassword.setDeviceNo(null);
        tbDoorlockPassword.setThirdPasswordId(null);
        this.update(tbDoorlockPassword, wrapper);
    }
}
